#include "passone.hpp"
#include "functions.hpp"
bool pass_one(vector<string>& file,int& line_amount) {
	write("Pass One","1;34",true);
    //setup all the variables needed
    //it is the pointer to the start of the vector<string>
    vector<string>::iterator it = file.begin();
    int x;
    int y=0; // Changed to fix a random bug on windows where it wasn't initialized
    int z=0; // Changed to fix a random bug on windows where it wasn't initialized
    //int r;
    //line is the temporary string for the current line
    string line;
    //creates a string so that you know if it is inside a " or a '
    string delim = "\0";
    //creates a temporary string
    string tmp;
    //temporary var for output of integers
    stringstream s;
    //scan through file
    while (++it!=file.end()) {
        //line is the current line being checked
        //trim_right(line = *it,it-file.begin());
        line = *it;
        //sets the temporary variable to 0
        x=0;
        while(x!=(int)line.length()){
        	//scans for the first catch character
        	while (line.substr(x,1)!="#" &&line.substr(x,1)!=" " && line.substr(x,1)!="\t" && line.substr(x,1)!="\"" && line.substr(x,1)!="'" && x != (int)line.length())
			x++;
		//check to make sure it didn't halt just because it is the last character
		if( x != (int)line.length()){
			//store the current character for fast recall
			//also makes it easier to understand what's happening
			tmp = line.substr(x,1);
			//if no active delimiter is being searched for
			if(delim=="\0"){
				//start checking for something important
				switch(tmp[0]){
					//if it is a String
					case '"': case '\'':
						//store the type of starting character to
						//the delimiter variable
						delim = tmp;
						//store the current position in the string
						y = x;
						//store the current line
						z = it - file.begin();
						//increase position
						x++;
						break;
					//if it is #
					case '#':
						//check if it's #define and the start of the line
						if(x==0 && tolowercase(line.substr(x+1,6)) == "define"){
							string args;	//both the arguments
							string arg1;	//the first one
							string arg2;	//the optional second one
							//handle it here
							//get the line minus the #define
							args = line.substr(x+7);
							//strip leading whitespaces
							args = args.substr(args.find_first_not_of(" \t\r\n"));
							//strip ending whitespace
							args = args.substr(0,args.find_last_not_of(" \t\r\n")+1);
							//get the name of the define and store it as arg1
							arg1 = args.substr(0,args.find_first_of(" \t\r\n"));
							//if the define has a replacement value get it in arg2
							if(args.find_first_of(" \t\r\n")!=args.npos){
								args = args.substr(args.find_first_of(" \t\r\n"));
								arg2 = args.substr(args.find_first_not_of(" \t\r\n"));
							}
							//store the define here for testing
							//write to verbose the define
							write(itoa(it-file.begin())+" :\t"+arg1+" -defined- "+arg2);
							
						}//add more here for other # tests
						//increase the pointer so we avoide infinity loops
						x++;
						break;
					case ' ':case '\t':
						//if it is a whitespace just move forward for now
						x++;
						break;
					default:
						//if no cases are triggered just move forward
						x++;
						break;
				}
			}else{
				//if a delimiter is in place start testing
				//will add a switch/case statement when I add others
				if(line.substr(x,1)==delim && line.substr(x-1,1)!="\\"){
					//reset delimeter
					delim="\0";
					//set up temporary string data storage
					string string_data;
					//if it is a multiline string error.
					if (it - file.begin()!=z && (delim=="\"" || delim=="'")){
						write("Error multiline string starting on line "+itoa(z),"31",true);
						return false;
					}
					/*r =  it - file.begin();
					write(itoa(z)+":"+itoa(y)+" to: "+itoa(r)+":"+itoa(x),"32");
					for (int i=z;i<=r;i++){
						line = file.at(i);
						//if whole line is still in string
						if(i!=z && i!=r){
							write(itoa(i)+" :\t"+line);
							string_data.append(line);
						//if it is the last line
						}else if(i==r&&i!=z){
							write(itoa(i)+" :\t"+line.substr(0,x));
							string_data.append(line.substr(0,x));
						//if it is the first line
						}else if(i==z&&i!=r){
							write(itoa(i)+" :\t"+line.substr(y));
							string_data.append(line.substr(y));
						//if it is the only line
						}else{
							int t = line.length()-y-(line.length()-x)+1;
							write(itoa(i)+" :\t"+line.substr(y,t));
							string_data.append(line.substr(y,t));
						}
						
					}*/
					//get the position of the ermm...let me look...the length of the string actually
					int t = line.length()-y-(line.length()-x)+1;
					//write to verbose the string that was grabbed and the line it was on
					write(itoa(z)+" :\t"+line.substr(y,t));
					//store the string
					string_data = line.substr(y,t);
					//if it can't convert the string error
					if(!convert_string(string_data)){
						write("Error invalid character on line "+itoa(z),"31",true);
						return false;
					}
					//otherwise write the converted string to verbose
					write("\t"+string_data);
					//line = line.substr(0,y)+string_data+line.substr(t);
					//store the line to tmp
					tmp = line;
					//get the first part of the line, store it plus the new data to line
					line = line.substr(0,y)+string_data;
					//if the string isn't the end of the line
					if(x<(int)tmp.length()-1)
						line += tmp.substr(x+1); //add the last part after the replaced string
					//erase the current line
					file.erase(it);
					//insert the changed line
					file.insert(it,line);
					//get the new x position
					x = y+string_data.length();
					//keep it from erroring out if there is nothing after the last '/"
					if(x==(int)line.length())
						x--;
				}
				//increase position so we don't keep checking the same spot
				x++;
			}
		}
        }
    }
    //no error, everything went well
    return true;
}
bool convert_string(string& s){
	//setup temporary strings
	string n;
	string t;
	//loop through the line, minus the first " and the last "
	for(int i=1; i<(int)s.length()-1;i++){
		//store the current character to t
		t = s.substr(i,1);
		//if it's a backslash
		if(t=="\\" /*|| !in_array(t[0],ascii)*/ ){
			if(s.substr(i,1)=="\\"){
				//convert correctly
				if(s.substr(i+1,1)=="t")
					t = "\t";
				else if(s.substr(i+1,1)=="n")
					t = "\n";
				else if(s.substr(i+1,1)=="r")
					t = "\r";
				else if(s.substr(i+1,1)=="\\")
					t = "\\";
				else if(s.substr(i+1,1)=="\"")
					t = "\"";
				else	//if nothing error
					return false;
				//increase position past the letter
				i++;
			}else{
				//not a valid character error
				write("|"+s.substr(i,1)+"|");
				return false;
			}
		}
		//add $ to the end of n
		n.append("$");
		//add the hex after it
		n.append(itoa(t[0],16));
		//if not the end of the string add a ,
		if(i!=(int)(s.length()-2))
			n.append(",");
	}
	//store the converted string to s
	s = n;
	//return without errors
	return true;
}